﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.Plugin.Contract;
using DWF.Studio.Plugin.Contract.Exceptions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DWF.Studio.Plugin.Contract
{
    /// <summary>
    /// 插件参数
    /// </summary>
    public sealed class PluginParameters : IEnumerable
    {
        private Dictionary<string, object> _parameters = null;

        public PluginParameters()
        {
            _parameters = new Dictionary<string, object>();
        }

        public IEnumerator GetEnumerator()
        {
            return _parameters.GetEnumerator();
        }

        public int Count
        {
            get
            {
                return _parameters.Count;
            }
        }

        /// <summary>
        /// 获取参数
        /// </summary>
        /// <param name="parameterName"></param>
        /// <returns></returns>
        public object Get(string parameterName)
        {
            if (this._parameters.ContainsKey(parameterName))
            {
                return this._parameters[parameterName];
            }

            return null;
        }

        /// <summary>
        /// 获取参数
        /// </summary>
        /// <param name="parameterName"></param>
        /// <returns></returns>
        public T Get<T>(string parameterName)
        {
            T result = default(T);

            if (this._parameters.ContainsKey(parameterName))
            {
                result = (T)this._parameters[parameterName];
            }

            return result;
        }

        /// <summary>
        /// 设置参数
        /// </summary>
        /// <param name="parameterName"></param>
        /// <param name="parameterValue"></param>
        public void Set(string parameterName, object parameterValue)
        {
            if (_parameters.ContainsKey(parameterName))
            {
                _parameters[parameterName] = parameterValue;
            }
        }

        /// <summary>
        /// 是否包含参数名
        /// </summary>
        /// <param name="parameterName"></param>
        /// <returns></returns>
        public bool Contains(string parameterName)
        {
            return this._parameters.ContainsKey(parameterName);
        }

        /// <summary>
        /// 转换成字典
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, object> ToDictionary()
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();

            foreach (string key in dic.Keys)
                dic.Add(key, dic[key]);

            return dic;
        }

        /// <summary>
        /// 添加参数
        /// </summary>
        /// <param name="parameterName"></param>
        /// <param name="parameterValue"></param>
        public void Add(string parameterName, object parameterValue)
        {
            this._parameters.Add(parameterName, parameterValue);
        }
    }
}
